package individual;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Point;
import java.util.ArrayList;
import java.util.List;

import behavior.Behavior;
import behavior.Parameters;

import Environment.*;
import utilities.Vector;
import debugger.*;

/**
 * This is type of MovingEntity whose subclasses have a particular BoidBehavior that
 * determines how it moves by calculating its next Velocity and thus Center based on
 * its neighboring Boids
 * 
 * @author ga-youngjoung
 *
 */
public abstract class Boid extends MovingEntity {
	//these are protected because subclasses must have access to them
	protected Vector myNextCenter;
	protected Vector myNextVelocity;
    static Parameters myParameters;


    /**
     * Construct a shape at the given position, with the given velocity, 
     * size, and color.  Also a size for its trail (if implemented).
     *
     * @param center position of the shape
     * @param size dimensions of the shape
     * @param velocity speed and direction of the shape
     * @param color color of the shape
     * @param trailSize number of lines to draw following the shape
     */
    public Boid (Vector center, Dimension size, Vector velocity, Environment environment, Parameters parameter, Debugger debugger)
    {
        myCenter = new Vector(center);
        myNextCenter = new Vector(center);
        mySize = new Dimension(size.width, size.height);
        myNextVelocity = new Vector(velocity);
        myVelocity = new Vector(velocity);
        myColor = Color.black;
        myEnvironment = environment;
        myTrail = new ArrayList<Vector>();
        myParameters = parameter;
        myDebugger = debugger;
        isDead = false;
    }


    /**
     * Describes how to "animate" the shape by changing its state.
     *
     * Currently, moves by the current velocity.
     *
     * @param bounds which encloses this shape
     */
    
    public abstract void update (List<MovingEntity> allIndividuals);
    
    
    /**
     * Describes how to draw the shape on the screen.
     *
     * Currently, draw a trail of the shape's previous positions if it exists.
     * 
     * @param pen used to paint shape on the screen
     */
    public void paint (Graphics pen)
    {
    	pen.setColor(myColor);
        pen.fillOval((int) (myNextCenter.x - mySize.width / 2),
                (int) (myNextCenter.y - mySize.height / 2),
                mySize.width, mySize.height);
    	myCenter = new Vector(myNextCenter);
    	myVelocity = new Vector(myNextVelocity);
    	
        if (myDebugger.myMaxTrail > 0 && myTrail.size() > 0)
        {
            drawTrail(pen);
        }
    }
    
    /**
     * Draws a trail connecting the shape's previous positions.
     * 
     * @param pen used to paint shape on the screen
     */
    private void drawTrail (Graphics pen)
    {
        pen.setColor(myColor);
        // draw trail
        Vector previous = myTrail.get(0);
        for (int k = 1; k < myTrail.size(); k++)
        {
            Vector current = myTrail.get(k);
            pen.drawLine((int)previous.x, (int)previous.y, (int) current.x, (int)current.y);
            previous = current;
        }
    }
}
